package org.aiforum.web.common;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.aiforum.frountend.common.CookieHelper;
import org.aiforum.frountend.service.ContactService;
import org.aiforum.frountend.service.PlateService;
import org.aiforum.frountend.service.ReplyService;
import org.aiforum.frountend.service.TopicService;
import org.aiforum.frountend.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * Action涓婁笅鏂�
 * 
 * 璇ョ被鍖呭惈Request,Response,Session,Cookie鎿嶄綔鍙奡truts2涓婁笅鏂�
 * 
 * 瀹炵幇ServletRequestAware,ServletResponseAware鎺ュ彛鍜屾柟娉�,
 * 璁㏒truts2鑷姩灏唕equest鍜宺esponse娉ㄥ叆鍒伴�氱敤Action涓��
 * 
 * @author VIC
 *
 */
public abstract class AiActionContext extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	
	private static final long serialVersionUID = 1L;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	
	protected String message;
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

	// 鍒涘缓鍏叡鏃ュ織瀵硅薄
	protected Logger logger = LogManager.getLogger(getClass());
	
	// 鍒涘缓涓氬姟鎺ュ彛瀵硅薄
	@Autowired
	protected UserService userService;
	@Autowired
	protected PlateService plateService;
	// 创建业务接口对象
	@Autowired
	protected ContactService contactService;
	@Autowired
	protected TopicService topicService;
	@Autowired
	protected ReplyService replyService;
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		this.response.setCharacterEncoding("UTF-8");
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		try {
			this.request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
		
		/**
		 * 娣诲姞鏁版嵁鍒癝ession
		 * @param name
		 * @param value
		 */
		protected void setSession(String name,Object value){
			request.getSession().setAttribute(name, value);
		}
		
		/***
		 * 鑾峰彇Session涓暟鎹�
		 * @param name
		 * @param clz
		 * @return
		 */
		protected <T> T getSession(String name,Class<T> clz){
			Object object = request.getSession().getAttribute(name);
			if(null != object){
				return (T)object;
			}
			return null;
		}
		
		/**
		 * 娉ㄩ攢Sesion
		 */
		protected void invalidateSession(){
			// 鍒犻櫎Session涓殑灞炴��
			HttpSession session = request.getSession();
			Enumeration<String> attributeNames = session.getAttributeNames();
			while (attributeNames.hasMoreElements()) {
				String name = attributeNames.nextElement();
				session.removeAttribute(name);
			}
			// 娉ㄩ攢Session
			session.invalidate();
		}
		
		/**
		 * 鑾峰彇SesionID
		 * @return
		 */
		protected String getSessionId(){
			return request.getSession().getId();
		}
		
		/**
		 * 鑾峰彇璇锋眰鍙傛暟
		 * 渚嬪鑾峰彇account鐨勫�尖�渁dmin鈥�
		 * http://domain/get?account=admin
		 * 
		 * @param name 鍙傛暟鍚�
		 * @return
		 */
		protected String getParames(String name){
			return request.getParameter(name);
		}
		
		/**
		 * 鑾峰彇璇锋眰鏌ヨ瀛楃涓�
		 * 渚嬪鑾峰彇鏌ヨ瀛楃涓测�渁ccount=admin鈥�
		 * http://domain/get?account=admin
		 * @return
		 */
		protected String getQueryString(){
			return request.getQueryString();
		}
		
		/**
		 * 鑾峰彇璇锋眰澶�
		 * @param name
		 * @return
		 */
		protected String getHeader(String name){
			return request.getHeader(name);
		}
		
		/**
		 * 璁剧疆璇锋眰澶�
		 * @param name
		 * @param value
		 */
		protected void setHeader(String name,String value){
			response.addHeader(name, value);
		}
		
		/**
		 * 鑾峰彇璇锋眰URL
		 * @return
		 */
		protected String getRequestPath(){
			return request.getRequestURI();
		}
		
		/**
		 * 获取Cookie
		 * @param name
		 * @return
		 */
		protected String getCookie(String name){
			return CookieHelper.getCookie(name, request);
		}
		
		/**
		 * 设置Cookie
		 * @param name
		 * @param value
		 */
		protected void setCookie(String name,String value){
			CookieHelper.setCookie(name, value, response);
		}
		
		protected void delCookie(String name) {
			CookieHelper.delCookie(name, response);
		}
		
		/**
		 * 杈撳嚭鏁版嵁鍒版祻瑙堝櫒
		 * @param context
		 */
		protected void writer(String context){
			try {
				response.getWriter().write(context);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
}
